/***************************************************************************
 创建者: hualei
 开始时间: 2018.10.15
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2018.10.15 类的具体实现代码编写

 ***************************************************************************/
#include "loadmapinstructionlogic.h"
#include "programengine.h"
#include "programset.h"
#include "mccontrollerthread.h"
#include "GeneralDefine.h"
#include "messagelog.h"

LoadMapInstructionLogic::LoadMapInstructionLogic(int robotIdIn, ProgramSet *programSetIn, ProgramEngine *programEngineIn, McControllerThread *programMcControllerIn)
{
    robotId = robotIdIn;
    programSet = programSetIn;
    programEngine = programEngineIn;
    programMcController = programMcControllerIn;

}

E_PROGRAM_LOOP_RESULT LoadMapInstructionLogic::startLoadMap(int programNumIn, const LoadMapInstruction &instructionIn, int lineIndex)
{
    if(instructionIn.instructionType != LOAD_MAP)
    {
        addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12052, robotId
                , programEngine->getRunningLineFloat() );
        programEngine->startJump(programNumIn,instructionIn.jmpLabel);
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }

    Q_UNUSED(lineIndex);
    int resultFlag;
    // 加载地图
    programMcController->getControllerInterpreter()->loadMap(instructionIn.mapFileName.toStdString(),resultFlag);

    //根据地图的复杂程度，可能需要等待几秒种才能加载成功。
    sleep(8);

    if(resultFlag != 1)
    {
        addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12051, robotId
                , programEngine->getRunningLineFloat() );
        programEngine->startJump(programNumIn,instructionIn.jmpLabel);
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }

    programEngine->calculateNextLineToRun(programNumIn);

    return programEngine->isTheLastInstruction(programNumIn);

}

void LoadMapInstructionLogic::addMsg(int messageLevel, string componentName, string messageType, int messageCode, int robotId, int parameter1, int parameter2, int parameter3, int parameter4, QString message)
{
    Q_UNUSED(parameter2);
    Q_UNUSED(parameter3);
    Q_UNUSED(parameter4);

    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;

    QString infomationStr;

    switch(messageCode)
    {

       case 12051:
       {

            infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("第") +
                QString::number(parameter1+1) +
                QObject::tr("行运行出现异常:加载地图失败!")+message;

            break;
        }
    case 12052:
    {

         infomationStr = QObject::tr("机器人:") +
             QString::number(robotId) +
             QObject::tr("第") +
             QString::number(parameter1+1) +
             QObject::tr("行运行出现异常:指令类型错误!")+message;

         break;
     }
    default:
    {

    }
    }
    tmpMsg.MessageInformation = infomationStr.toStdString();

    // qDebug()<<"53444444444444444444444"<< tmpMsg.messageCode;
    MessageLog::getInstance()->addMessage(tmpMsg);
}
